﻿using System;
using System.Data;
using System.Data.Common;
using QzEbook.Common;
using System.Text;

/// <summary>
///DetaiBookView 的摘要说明
/// </summary>
/// 

namespace QzEbook.AccessObject
{
    public class DetaiBookView:abstractAccess<DetaiBookView>
    {
        private int bId;
        public int BId
        {
            get { return bId; }
            set { bId = value; }
        }
        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        private string path;

        public string Path
        {
            get { return path; }
            set { path = value; }
        }
        private int price;

        public int Price
        {
            get { return price; }
            set { price = value; }
        }
        private DateTime postDate;

        public DateTime PostDate
        {
            get { return postDate; }
            set { postDate = value; }
        }

        public DetaiBookView()
        {
        }

        /// <summary>
        /// 选取当前
        /// </summary>
        /// <returns></returns>
        protected override DataTable selectTable()
        {
            DbCommand command = CreateCommand("detailBookView");
            addParameter(command, "@BID",bId);
            return DataAccess.ExcuteSelectCommand(command);
        }

        protected override DetaiBookView bindRow(DataRow row)
        {
            DetaiBookView book = new DetaiBookView();
            book.BId=int.Parse(row["Bid"].ToString());
            book.Title=row["BTitle"].ToString();
            book.Path=row["BThumb"].ToString();
            book.PostDate = DateTime.Parse(row["BDate"].ToString());
            book.price = int.Parse(row["BPrice"].ToString());
            return book;
        }

        /// <summary>
        /// 选取前一个
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public DetaiBookView selectForward(DateTime time)
        {
            DbCommand command = DataAccess.CreateCommand();
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT top 1 * FROM  Qz_Book WHERE");
            sb.AppendFormat(" BDate>'{0}' and Bid<>{1} and",time,bId);
            sb.Append(" BIsVisible<>0 and BAuditing=3");
            sb.Append(" order by BDate ASC");

            DataTable table= DataAccess.ExcuteSelectCommand(sb.ToString());
            if (table.Rows.Count == 0) return null;
            return bindRow(table.Rows[0]);
        }

        /// <summary>
        /// 选取后一个
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public DetaiBookView selectBackward(DateTime time)
        {
            DbCommand command = DataAccess.CreateCommand();

            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT top 1 * FROM  Qz_Book WHERE");
            sb.AppendFormat(" BDate<'{0}' and Bid<>{1} and", time,bId);
            sb.Append(" BIsVisible<>0 and BAuditing=3");
            sb.Append(" order by BDate DESC");


            DataTable table= DataAccess.ExcuteSelectCommand(sb.ToString());
            if (table.Rows.Count == 0) return null;
            return bindRow(table.Rows[0]);
        }
    }
}